﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Model
{
    public abstract class AMRpcHandler<Req,Resp> : IMHandler where Req : ARequest where Resp : AResponse
    {
        public Type GetMessageType()
        {
            return typeof(Req);
        }

        public void Handle(Session session, AMessage message)
        {
            Req req = message as Req;
            if(req == null)
            {
                Log.Error($"消息转换失败:{message.GetType().Name} to {typeof(Req).Name}");
            }

            this.Do(session, req, resp =>
            {
                // 可能session已经断开。。。
                if(session.Id == 0)
                {
                    return;
                }
                // 回应
                resp.RpcId = req.RpcId;
                session.Reply<Resp>(resp);
            });
        }
        protected abstract void Do(Session session,Req request,Action<Resp> reply);
    }
}
